/**
 * Physics Laws Module - TypeScript Version
 */

export interface IPhysicsLaws {
  ohmsLaw: (v: number, i: number, r: number) => number;
  kirchhoffCurrent: (node: {
    connections: Array<{
      direction: 'in' | 'out';
      current: number;
    }>;
  }) => boolean;
  kirchhoffVoltage: (loop: {
    elements: Array<{ voltageDrop: number }>;
  }) => boolean;
  powerCalculation: (v: number, i: number) => number;
  voltageDivider: (vIn: number, r1: number, r2: number) => number;
  currentDivider: (iIn: number, r1: number, r2: number) => number;
}

export const PhysicsLaws: IPhysicsLaws = {
  ohmsLaw: (v, i, r) => i * r,
  
  kirchhoffCurrent: (node) => {
    let sumIn = 0, sumOut = 0;
    node.connections.forEach(conn => {
      if (conn.direction === 'in') sumIn += Math.abs(conn.current);
      else sumOut += Math.abs(conn.current);
    });
    return Math.abs(sumIn - sumOut) < 1e-6; // Allow for floating point errors
  },
  
  kirchhoffVoltage: (loop) => {
    let sumVoltage = 0;
    loop.elements.forEach(el => {
      sumVoltage += el.voltageDrop;
    });
    return Math.abs(sumVoltage) < 1e-6; // Allow for floating point errors
  },
  
  powerCalculation: (v, i) => v * i,
  
  voltageDivider: (vIn, r1, r2) => vIn * (r2 / (r1 + r2)),
  
  currentDivider: (iIn, r1, r2) => iIn * (r1 / (r1 + r2))
};